package com.ehome.poct.base.controller.device;

import com.ehome.core.util.BeanConvertUtil;
import com.ehome.poct.base.api.dto.device.*;
import com.ehome.poct.base.api.virtual.TestBloodQualityFactoryDataService;
import com.ehome.poct.base.api.virtual.TestDeviceParamService;
import com.ehome.poct.base.api.virtual.TestDeviceService;
import com.ehome.poct.base.api.virtual.device.DeviceBindService;
import com.ehome.poct.base.api.vo.device.DeviceBindVO;
import com.ehome.poct.base.api.vo.device.TestDeviceClinicVO;
import com.ehome.poct.base.api.vo.device.TestDeviceParamVO;
import com.ehome.poct.base.api.vo.device.TestDevicePrinterVO;
import com.ehome.poct.base.api.vo.device.TestDeviceVO;
import com.ehome.web.controller.AbstractBaseController;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 设备
 * 设备 表控制器
 *
 * @author 瑶苏
 * @since 2022/03/03 11:20
 **/
@Slf4j
@Api(tags = "设备管理")
@RestController
@AllArgsConstructor
@RequestMapping("/device")
public class TestDeviceController extends AbstractBaseController<TestDeviceVO, TestDeviceDTO> {
    private final TestDeviceService testDeviceService;
    private final TestDeviceParamService testDeviceParamService;
    private final TestBloodQualityFactoryDataService testBloodQualityFactoryDataService;
    private final DeviceBindService deviceBindService;


    @ApiOperation("根据sno获取设备")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/getBySno/{sno}")
    public TestDeviceVO getBySno(@PathVariable String sno){
        return testDeviceService.getBySno(sno);
    }
    
    @ApiOperation("根据sno获取设备诊所相关信息")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/getDeviceBySno")
    public List<TestDeviceClinicVO> getDeviceBySno(@RequestBody TestDeviceClinicDTO testDeviceClinicDTO){
        return testDeviceService.getDeviceBySno(testDeviceClinicDTO);
    }
    
    
    @ApiOperation("判断设备号是否存在")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/isExistence/{sno}")
    public Boolean isExistence(@PathVariable String sno){
        return testDeviceService.isExistence(sno);
    }

    @ApiOperation("新增设备")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/addDevice")
    public Boolean addDevice(@RequestBody AddDeviceDTO addDeviceDTO){
        return testDeviceService.addDevice(addDeviceDTO.getSno(), addDeviceDTO.getType());
    }

    @ApiOperation("修改设备状态")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/updateStatus")
    public void updateStatus(@RequestBody DeviceStatusDTO dto ){
        testDeviceService.editStatus(dto);
    }

    @ApiOperation("工厂模式")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/addFactoryMode")
    public boolean addFactoryModee(@RequestBody AddFactoryModeDTO addFactoryModeDTO) {
        return testBloodQualityFactoryDataService.AddFactoryMode(addFactoryModeDTO);
    }

    @ApiOperation("新增和修改设备配置文件")
    @ApiOperationSupport(author = "瑶苏")
    @PostMapping("/setDeviceParam")
    public boolean setDeviceParam(@RequestBody TestDeviceParamDTO testDeviceParamDTO ) {
        return testDeviceParamService.setDeviceParam(testDeviceParamDTO);
    }

    @ApiOperation("获取设备的配置信息(对应新库)")
    @ApiOperationSupport(author = "瑶苏")
    @GetMapping("/getDeviceParam")
    public TestDeviceParamVO getDeviceParamNew(@RequestBody TestDeviceParamDTO testDeviceParamDTO ) {
        return testDeviceParamService.getDeviceParamNew(testDeviceParamDTO);
    }
    @ApiOperation("获取设备的配置信息")
    @ApiOperationSupport(author = "cq")
    @PostMapping("/getBySnoAndType")
    public TestDeviceVO getBySnoAndType(@RequestBody  DeviceClinicDTO param ) {
        return testDeviceService.getByClinic(param.getClinicId(),param.getDeviceType());
    }

    @ApiOperation("获取所有绑定了设备的诊所用户ID")
    @ApiOperationSupport(author = "cq")
    @GetMapping("/bind/clinic")
    public List<Long> getAllBindClinic() {
        return testDeviceService.getAllBindClinic();
    }

    @ApiOperation("更新设备连接打印机信息")
    @ApiOperationSupport(author = "muqiu")
    @PostMapping("/devicePrinter")
    public void devicePrinter(@RequestBody TestDevicePrinterDTO testDevicePrinterDTO){
        testDeviceService.devicePrinter(testDevicePrinterDTO);
    }

    @ApiOperation("获取设备连接打印机信息")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/getDevicePrinter/{sno}")
    public List<TestDevicePrinterVO> getDevicePrinter(@PathVariable String sno){
        return testDeviceService.getDevicePrinter(sno);
    }

    @ApiOperation("获取设备最新的绑定记录")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/binding/{sno}")
    public DeviceBindVO getLastBindingClinic(@PathVariable String sno){
        return BeanConvertUtil.convertBean(deviceBindService.getLast(sno), DeviceBindVO.class);
    }

    @ApiOperation("删除设备云端配置文件")
    @ApiOperationSupport(author = "wyc")
    @DeleteMapping("/delete/{sno}")
    public int deleteBySno(@PathVariable String sno){
        return testDeviceParamService.deleteBySno(sno);
    }

    @ApiOperation("烧录设备入库")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/input/storage")
    public void inputStorage(@RequestBody TestDeviceDTO dto){
        testDeviceService.insetOrUpdate(dto);
    }
}

